﻿using System;

namespace Codemonk.Common.ObjectModel
{
    public class TimerObject<T>
    {
        public TimerObject()
        {
            p_birthday = DateTime.Now;
        }

        private DateTime p_birthday;

        public T Value
        {
            get;
            set;
        }


        public void ResetBirthday()
        {
            p_birthday = DateTime.Now;
        }

        public virtual DateTime Birthday
        {
            get
            {
                return p_birthday;
            }
            protected
            set
            {
                p_birthday = value;
            }
        }

        public virtual TimeSpan Elapsed
        {
            get
            {
                return DateTime.Now - p_birthday;
            }
        }

        public virtual double ElapsedMilliseconds
        {
            get
            {
                return Elapsed.TotalMilliseconds;
            }
        }

        public virtual long ElapsedTicks
        {
            get
            {
                return Elapsed.Ticks;
            }
        }

        public static implicit operator TimerObject<T>(T value)
        {
            return new TimerObject<T>
            {
                Value = value
            };
        }

        public static implicit operator T(TimerObject<T> value)
        {
            return value == null ? default(T) : value.Value;
        }
    }
}
